package GUIStudy.Project.PuzzleGame.WindowParts;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.util.Random;

public class GameJFrame extends JFrame {
    int[][] arr = new int[4][4];

    public GameJFrame() {
        setFrame();

        setBar();

        initArr();

        setLabel();

        //设置界面可见
        this.setVisible(true);
    }

    private void setFrame() {
        //JFrame基础设置
        this.setSize(603, 680);
        this.setTitle("拼图游戏^1.0");
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);
    }

    private void setBar() {
        //菜单栏
        JMenuBar bar = new JMenuBar();

        //菜单选项
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutUsJMenu = new JMenu("关于我们");

        //菜单子选项
        JMenuItem gameRestartItem = new JMenuItem("重开游戏");
        JMenuItem relogItem = new JMenuItem("重新登录");
        JMenuItem closeGameItem = new JMenuItem("关闭游戏");
        JMenuItem accountItem = new JMenuItem("公众号");

        //菜单子选项添加
        functionJMenu.add(gameRestartItem);
        functionJMenu.add(relogItem);
        functionJMenu.add(closeGameItem);
        aboutUsJMenu.add(accountItem);

        //菜单选项添加
        bar.add(functionJMenu);
        bar.add(aboutUsJMenu);

        //设置菜单
        this.setJMenuBar(bar);
    }

    private void initArr() {
        Random r = new Random(16);
        int[] disrupt = new int[16];
        for (int i = 0; i < disrupt.length; i++) {
            disrupt[i] = i;
        }
        for (int i = 0; i < disrupt.length; i++) {
            int exchange = r.nextInt(16 - i) + i;
            int temp = disrupt[i];
            disrupt[i] = disrupt[exchange];
            disrupt[exchange] = temp;
        }
        for (int i = 0; i < disrupt.length; i++) {
            arr[i / 4][i % 4] = disrupt[i];
        }
    }

    private void setLabel() {
        JLabel background = new JLabel(new ImageIcon("git-container-java/src/GUIStudy/" +
                "Project/PuzzleGame/WindowParts/Pictures/background.png"));
        background.setBounds(40, 40, 508, 560);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                JLabel label = new JLabel(new ImageIcon("git-container-java/src/GUIStudy/Project" +
                        "/PuzzleGame/WindowParts/Pictures/animal/animal1/" + arr[i][j] + ".jpg"));
                label.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                label.setBorder(new BevelBorder(BevelBorder.LOWERED));
                this.add(label);
            }
        }
        this.add(background);
    }
}
